package com.carkeeper.system.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.pojo.Employee;
import com.carkeeper.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据id查询
     * 修改用户前的数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Map<String, Object> resultMap = userService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", resultMap);
    }


    /**
     * 修改用户
     *
     * @param employee
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Employee employee, @PathVariable Long id) {
        Result<Object> result = new Result<>();
        if (employee.getUsername() == null | employee.getPassword() == null) {
            return new Result(false, StatusCode.OK, "修改失败");
        }
        userService.update(employee);
        return new Result(true, StatusCode.OK, "修改成功");
    }



    /**
     * 锁定/解锁
     *
     * @param employee
     * @param id
     * @return
     */
    @PutMapping("/lock/{id}")
    public Result lock(@RequestBody Employee employee, @PathVariable Long id) {
        return userService.lock(employee);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Map resultMap = userService.findPage(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", resultMap);
    }

}
